﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 试卷试题操作控制器
    /// </summary>
    public class ExamPaperDetailController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.ExamPaperDetail bll = new BLL.ExamPaperDetail();
        
        /// <summary>
        /// 试卷试题维护 gaoqihang
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [Route("api/ExamPaperDetail/UpdatePaperQuestions")]
        [HttpPost]
        public IHttpActionResult UpdatePaperQuestions(dynamic obj)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                int EPID = Convert.ToInt32(obj.EPID);
                string EQIDS = Convert.ToString(obj.EQIDS);
                log.Info($"EPID={EPID},EQIDS={EQIDS}");
                List<Model.ExamPaperDetail> modellist = new List<Model.ExamPaperDetail>();
                Model.T_UserInfo userInfo = LoginUser.UserInfo;
                Dictionary<int, int> dic = new Dictionary<int, int>();
                foreach (var item in EQIDS.Split('|'))
                {
                    dic.Add(int.Parse(item.Split('-')[0]), int.Parse(item.Split('-')[1]));
                }
                int i = 1;
                foreach (var item in dic.OrderBy(d => d.Value))
                {
                    modellist.Add(new Model.ExamPaperDetail()
                    {
                        EPID = EPID,
                        EQID = item.Key,
                        EPDOrder = i,
                        EPDAddEmpID = userInfo.UserID,
                        EPDAddEmpName = userInfo.UserName,
                        EPDAddDateTime = DateTime.Now
                    });
                    i++;
                }
                bll.UpdatePaperQuestions(EPID, modellist);
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "保存成功";
                resultMsg.Count = 1;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 根据试卷id获取试卷试题信息
        /// </summary>
        /// <param name="EPID"></param>
        /// <returns></returns>
        [Route("api/ExamPaperDetail/GetPaperQuestions")]
        [HttpGet]
        public IHttpActionResult GetPaperQuestions(int EPID)
        {
            log.Info($"EPID={EPID}");

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                DataTable dt = bll.GetPaperQuestions(EPID).Tables[0];
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = dt.Rows.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
                resultMsg.Data = dt;
                resultMsg.Count = dt.Rows.Count;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
    }
}
